import { parse } from 'url';
import { TableListItem, TableListParams, DepartmentItem } from './data.d';

const courier_name = ['顺丰速递', '申通速运', '圆通快递', '德邦物流', '韵达快递', '邮政速运'];

function getCourierCompanyDeparment(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: TableListItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const count = 50;
  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: TableListItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: starNum + index + 1,
      createUser: 1,
      createTime: '2010-11-20',
      createUserName: '张三丰',
      gardenId: 1,
      orgId: 1,
      orgName: '工程组',
      orgParentName: '中科富创-采购与工程部',
      status: 1,
      updateTime: '2010-10-10',
      updateUser: 1,
      updateUserName: '张无忌',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count,
    },
  };

  return res.json(result);
}

function getSaveDepartmentList(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: DepartmentItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const count = 50;
  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: DepartmentItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: starNum + index + 1,
      name: courier_name[Math.floor(Math.random() * 6)],
      code: '',
      autonomy: 0,
      status: 1,
      update_user: '张三丰',
      level: 1,
      create_user: '张三丰',
      is_branch: 0,
      parent_id: 1,
      parentName: '中科富创',
      create_time: '2019-08-30 12:00:00',
      update_time: '2019-08-30',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      count,
      list: dataSource,
    },
  };

  return res.json(result);
}

export default {
  'GET /garden/gardenDepartList': getCourierCompanyDeparment,
  'GET /org/allOrgListAndParent': getSaveDepartmentList,
};
